package com.yy.utils.file;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import com.yy.enums.FileBusinessTypeEnum;
import com.yy.utils.DateUtils;
import com.yy.utils.PropertiesUtil;
import com.yy.utils.exception.RRException;
import com.yy.utils.data.RegExpValidatorUtils;
import com.yy.validator.Assert;




/**
 * 文件上传
 * @author yy
 * @date
 */
public class FileUploadUtil {
	private static final Logger log = Logger.getLogger(FileUploadUtil.class);
	
	/**
	 * 删除文件
	 * @param fileName 文件名称
	 * @param dirName 目录
	 * @param time 文件储存时间
	 */
	public static void deleteFile(String dirName,String fileName) {
		String oldFilePath = String.format("%s%s%s%s%s", PropertiesUtil.getImgLocalPath(), File.separator,
				dirName, File.separator, fileName);
		log.error(oldFilePath);
		File old = new File(oldFilePath);
		old.delete();
    }
	
	/**
	 * 图片上传改名 带缩略图
	 * @param uploadImage 图片对象
	 * @param dirName 目录
	 * @param thumbnailSize 缩略图后缀
	 * @param w,h 缩略图高宽
	 * @return 图片名称
	 */
	public static String reportImgName(MultipartFile file,String dirName,String fileName,String thumbnailSize,int w, int h) {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		Assert.isBlank(dirName, "上传目录为空");
		//文件名
		String reportImgName = file.getOriginalFilename();
		if(!ImageUtil.imageName(reportImgName)){
			log.error("图片格式错误" + reportImgName);
			//return null;
			throw new RRException("图片格式错误");
		}
		//目录 手动获取目录名称  banner
		//String directory = FileBusinessTypeEnum.Banner.getCode();
		//图片名称参数名称为空，则自动改名
		String imageName = null;
		if(StringUtils.isBlank(fileName)){
			//图片编码(时间戳)
			long coding = System.currentTimeMillis();
			//文件后缀
			int imageIndex=reportImgName.indexOf(".");
			String imageSuffix= reportImgName.substring(imageIndex);
			//改名规则imageName 文件改名后  目录加时间戳
			imageName = String.format("%s%s%s",dirName,coding,imageSuffix);
		}else{
			imageName = fileName;
		}
		//生成带年月的文件目录
		String folder = ImageUtil.createFolder(PropertiesUtil.getImgLocalPath(), dirName);
		//文件目录
		String localFile=String.format("%s%s",folder , imageName);
		
		try {
			File newfile = new File(localFile);
			if (newfile.exists())
				newfile.delete();
			FileUtils.copyInputStreamToFile(file.getInputStream(), newfile);
			ImageUtil.thumbnailCutImage(folder, imageName, ImageUtil.thumbnailSuffix,
					thumbnailSize, w, h);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("上传失败" + reportImgName);
			//return  null;
			throw new RRException("上传失败");
		}
		//文件远程路径
		//String imgstr = String.format("%s%s%s%s%s",PropertiesUtil.getImgHttpAddress(),dirName,"/", new SimpleDateFormat("yyyy/MM").format(System.currentTimeMillis()),"/");
		//String imgPath = imgstr+imageName;
        return imageName;
    }
	/**
	 * 图片上传改名
	 * @param uploadImage 图片对象
	 * @param dirName 目录
	 * @return 图片名称
	 */
	public static String reportImgName(MultipartFile file,String dirName,String fileName) {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		Assert.isBlank(dirName, "上传目录为空");
		
		//文件名
		String reportImgName = file.getOriginalFilename();
		if(!ImageUtil.imageName(reportImgName)){
			log.error("图片格式错误" + reportImgName);
			//return null;
			throw new RRException("图片格式错误");
		}
		//目录 手动获取目录名称  banner
		//String directory = FileBusinessTypeEnum.Banner.getCode();
		//图片名称参数名称为空，则自动改名
		String imageName = null;
		if(StringUtils.isBlank(fileName)){
			//图片编码(时间戳)
			long coding = System.currentTimeMillis();
			//文件后缀
			int imageIndex=reportImgName.indexOf(".");
			String imageSuffix= reportImgName.substring(imageIndex);
			//改名规则imageName 文件改名后  目录加时间戳
			imageName = String.format("%s%s%s",dirName,coding,imageSuffix);
		}else{
			imageName = fileName;
		}
		//生成带年月的文件目录
		String folder = ImageUtil.createFolder(PropertiesUtil.getImgLocalPath(), dirName);
		//文件目录
		String localFile=String.format("%s%s",folder , imageName);
		
		try {
			File newfile = new File(localFile);
			if (newfile.exists())
				newfile.delete();
			FileUtils.copyInputStreamToFile(file.getInputStream(), newfile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("上传失败" + reportImgName);
			//return  null;
			throw new RRException("上传失败");
		}
		//文件远程路径
		//String imgstr = String.format("%s%s%s%s%s",PropertiesUtil.getImgHttpAddress(),dirName,"/", new SimpleDateFormat("yyyy/MM").format(System.currentTimeMillis()),"/");
		//String imgPath = imgstr+imageName;
        return imageName;
    }
	
	/**
	 * 视频上传-按时间储存-接口
	 * @param file 文件
	 * @param fileName 文件名 可不传
	 * @param dirName 文件储存目录
	 * @return 
	 */
	public static String fileVideoUpload(MultipartFile file,String dirName,String fileName) {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		Assert.isBlank(dirName, "上传目录为空");
		//文件名
		String reportVideoName = file.getOriginalFilename();
		//文件后缀
		int videoIndex=reportVideoName.indexOf(".");
		String videoSuffix= reportVideoName.substring(videoIndex);
		//判断文件是否是视频
		if(!RegExpValidatorUtils.isVideo(videoSuffix)){
			log.error("视频格式错误" + reportVideoName);
			//return null;
			throw new RRException("视频格式错误");
		}
		//图片名称参数名称为空，则自动改名
		if(StringUtils.isBlank(fileName)){
			//图片编码(时间戳)
			long coding = System.currentTimeMillis();
			//改名规则imageName 文件改名后  目录加时间戳
			reportVideoName = String.format("%s%s%s",dirName,coding,videoSuffix);
		}else{
			reportVideoName = fileName;
		}
		
		//生成带年月的文件目录
		String folder = ImageUtil.createFolder(PropertiesUtil.getVideoLocalPath(), dirName);
		//文件目录
		String localFile=String.format("%s%s",folder , reportVideoName);
		
		try {
			File newfile = new File(localFile);
			if (newfile.exists())
				newfile.delete();
			FileUtils.copyInputStreamToFile(file.getInputStream(), newfile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("上传失败" + reportVideoName);
			//return null;
			throw new RRException("上传失败");
		}
		//视频远程路径
		//String videostr = String.format("%s%s%s%s%s",PropertiesUtil.getVideoHttpAddress(),dirName,"/", new SimpleDateFormat("yyyy/MM").format(System.currentTimeMillis()),"/");
		//String videoPath = videostr+reportVideoName;
        return reportVideoName;
    }
}
